package com.atguigu.controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
import java.util.TreeMap;

/**
 * @Author chenxin
 * @date 2023/2/10
 * @Version 1.0
 */
public class BaseController {

    /**
     * 封装页面提交的分页参数及搜索条件
     * @param request
     * @return
     */
    public Map<String, Object> getFilters(HttpServletRequest request) {
        //获得所有的参数名字(key值)
        Enumeration<String> paramNames = request.getParameterNames();
        //实例化一个Map集合
        Map<String, Object> filters = new TreeMap();
        //循环paramNames
        while(paramNames != null && paramNames.hasMoreElements()) {
            //从容器内拿出一个key值
            String paramName = (String)paramNames.nextElement();
            //根据key值获取value值
            String[] values = request.getParameterValues(paramName);
            //将value值存储到Map集合内
            if (values != null && values.length != 0) {
                if (values.length > 1) {
                    //适配一个key值对应多个value值的情况
                    filters.put(paramName, values);
                } else {
                    //适配一个key值对应一个value的情况
                    filters.put(paramName, values[0]);
                }
            }
        }
        if(!filters.containsKey("pageNum")) {
            filters.put("pageNum", 1);
        }
        if(!filters.containsKey("pageSize")) {
            filters.put("pageSize", 5);
        }

        return filters;
    }
}
